package andev.game2048.Card;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.GridLayout;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import andev.game2048.GameView;

/**
 * Created by lzy1g on 2015/8/6.
 */
public class Card extends FrameLayout {
    private static Map<Integer, String> colorTable = new HashMap<Integer, String>() {
        {
            put(0, "#B2C0CC");
            put(2, "#DAE4EE");
            put(4, "#C8E0ED");
            put(8, "#79B1F2");
            put(16, "#6395F5");
            put(32, "#5F7CF6");
            put(64, "#3B5EF6");
            put(128, "#61CCED");
            put(256, "#61CCED");
            put(512, "#61CCED");
            put(1024, "#61CCED");
            put(2048, "#2EC2ED");
        }
    };
    public int row, col;
    private int number = 0;
    private TextView mTextView;

    public Card(Context context) {
        super(context);
        //I treat a card as empty card and add to emptyCardList when it's constructed,it will soon or later changed by number operate functions

        mTextView = new TextView(context);
        mTextView.setGravity(Gravity.CENTER);
        mTextView.setTextColor(Color.WHITE);
        mTextView.getPaint().setFakeBoldText(true);

        addView(mTextView);

        updateCard();
    }

    /**
     * @param number    设置当前Card的数字
     * @param cloneCard 如果cloneCard!=null，则会对cloneCard先启动一个translate动画，并在动画结束后再 'updateCard()'
     *                  如果cloneCard==null,则直接 'updateCard()'
     */
    public void setNumber(final int number, final Card cloneCard) {
        final int oldNumber = this.number;
        this.number = number;
        if (this.number != 0) {
            GameView.emptyCardList.remove(Card.this);
        } else GameView.emptyCardList.add(Card.this);

        if (cloneCard != null) {
            if (this.equals(cloneCard)) {
                updateCard();
                return;
            }
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    TranslateAnimation translateAnimation = new TranslateAnimation(0, getX() - cloneCard.getX(), 0, getY() - cloneCard.getY());
                    translateAnimation.setDuration(120);
                    cloneCard.startAnimation(translateAnimation);

                    translateAnimation.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            if (oldNumber != 0) scaleSelf(1, 1.4f, 200);
                            updateCard();

                            ((GridLayout) getParent()).removeView(cloneCard);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }
                    });
                }
            });
        } else {
            if (number != 0) scaleSelf(0.6f, 1, 200);
            updateCard();
        }
    }

    public int getNumber() {
        return number;
    }

    private void scaleSelf(final float fromScale, final float toScale, final int duration) {
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                ScaleAnimation scaleAnimation = new ScaleAnimation(fromScale, toScale, fromScale, toScale, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                scaleAnimation.setDuration(duration);
                startAnimation(scaleAnimation);
            }
        });
    }

    public void updateCard() {
        //text
        if (number != 0) mTextView.setText(Integer.toString(number));
        else mTextView.setText("");

        //size
        if (number <= 64) mTextView.setTextSize(GameView.cardWidth * 0.33f);
        else if (number > 64 && number < 1024) mTextView.setTextSize(GameView.cardWidth * 0.27f);
        else if (number >= 1024) mTextView.setTextSize(GameView.cardWidth * 0.18f);

        //background
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setCornerRadius(8);

        gradientDrawable.setColor(Color.parseColor(colorTable.get(number)));

        mTextView.setBackground(gradientDrawable);
    }

}
